package mn.idax.exchange.otc.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.otc.constant.Constant;

import java.io.UnsupportedEncodingException;

/**
 * @program: auth
 * @description: Encrypt
 * @author: zhuwei
 * @create: 2019/04/10 11:37
 */
public class Encrypt {

    /**
     *
     * @param authInfo
     * @return
     */
    public static String getToken(AuthInfo authInfo) {
        String token = null;
        try {
            token = JWT.create()
                    .withClaim("Id", authInfo.getId())
                    .withClaim("AccountType", authInfo.getAccountType())
                    .withClaim("LoginType", authInfo.getLoginType())
                    .withClaim("Mobile", authInfo.getMobile())
                    .withClaim("Email", authInfo.getEmail())
                    .withClaim("Ip", authInfo.getIp())
                    .withClaim("IsMfaPassed", authInfo.isMfaPassed())
                    .withClaim("Source", authInfo.getSource())
                    .withClaim("CreateTime",DateUtils.getDateToStr(authInfo.getCreateTime(),"yyyy-MM-dd'T'HH:mm:ss.S"))
                    .withClaim("ExpireHour", authInfo.getExpireHour())
                    // 使用了HMAC256加密算法。
                    // mysecret是用来加密数字签名的密钥。
                    .sign(Algorithm.HMAC256(Constant.PRIVATEKEY));

        } catch (JWTCreationException exception){
            //Invalid Signing configuration / Couldn't convert Claims.
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return token;
    }



}


